<?php

/**
 * searchresult actions.
 *
 * @package    Redsea20
 * @subpackage Profile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchresultActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  
  public function executeIndex(sfWebRequest $request)
  {
    $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {
          
          $str=$this->getRequestParameter('search');
          $this->researchs=Doctrine_Query::create()
                  ->select('odd.id,s.species,o.location,od.observationdepth,odd.quantity,o.observationdate,odd.tag')
                  ->from('observationdepthdetail odd')
                  ->leftJoin('odd.Species s')
		  ->leftJoin('odd.Observationdepth od')
		  ->leftJoin('od.Observationdepthattribute oda')
		  ->leftJoin('oda.Attribute a')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Entity rr')
                  ->where('tag LIKE ?', '%'.$str.'%')
                  ->ORwhere('s.species LIKE ?', '%'.$str.'%')
                  ->ORwhere('a.attributename LIKE ?', '%'.$str.'%')
                  ->ORwhere('oda.attributevalue LIKE ?', '%'.$str.'%')
                  ->ORwhere('r.title LIKE ?', '%'.$str.'%')
                  ->ORwhere('o.location LIKE ?', '%'.$str.'%')
                  ->ORwhere('rr.Firstname LIKE ?', '%'.$str.'%')
                  ->ORwhere('rr.Lastname LIKE ?', '%'.$str.'%')
                  ->ORwhere('substr((cast(o.observationdate as char(800))),1,10) LIKE ?', '%'.$str.'%')
                  ->orderby('o.Location,s.species')
                  ->execute();
//array(), Doctrine_Core::HYDRATE_ON_DEMAND
          $this->researchsattribute=Doctrine_Query::create()
                  ->select('od.id,o.location,od.observationdepth,o.observationdate')
		  ->from('observationdepth od')
                  ->leftJoin('od.Unit od_u')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Entity rr')
		  ->leftJoin('od.Observationdepthattribute oda')
                  ->leftJoin('oda.Attribute at')
                  ->leftJoin('oda.Unit oda_u')
                  ->where("od.id NOT IN (SELECT u.observationdepthid FROM observationdepthdetail u)")
                  ->ANDwhere("(at.attributename like '%".$str."%' OR oda.attributevalue  like '%".$str."%' OR rr.Firstname like '%".$str."%' OR rr.Lastname like '%".$str."%' OR r.title like '%".$str."%' OR o.location like '%".$str."%' or od_u.unit like '%".$str."%' OR oda_u.unit like '%".$str."%' OR substr((cast(o.observationdate as char(800))),1,10) like '%".$str."%')")
                  ->execute();
//
//
//
//


          $this->overlayImage=Doctrine_Query::create()
                  ->select('ro.id,ro.overlaytitle')
                  ->from('researchoverlay ro')
		  ->leftJoin('ro.Research r')
                  ->leftJoin('r.Entity rr')
                   ->where('r.title LIKE ?', '%'.$str.'%')
                  ->ORwhere('ro.overlaytitle LIKE ?', '%'.$str.'%')
                   ->ORwhere('rr.Firstname LIKE ?', '%'.$str.'%')
                  ->ORwhere('rr.Lastname LIKE ?', '%'.$str.'%')
                  ->execute();


      }
                  //  echo count($this->researchs_attribute);
                    //echo  $this->researchs->getSqlQuery();
		  //return sfView::NONE;
  }


public function executeHistory(sfWebRequest $request)
  {
    $str="";
      if ($this->getRequestParameter('searchhistory')!=null && $this->getRequestParameter('searchhistory')!="")
      {

          $str= explode("OLY", $this->getRequestParameter('searchhistory'));

          if ($str[0]!=null && $str[0]!="")
          {
          $this->researchs=Doctrine_Query::create()

                  ->select('odd.id,s.species,o.location,od.observationdepth,odd.quantity,o.observationdate,odd.tag')
                  ->from('observationdepthdetail odd')
                  ->leftJoin('odd.Species s')
		  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Entity rr')
                  ->where('odd.id In ('.$str[0].')')
                  ->orderby('o.Location,s.species')
                  ->execute();
          }

           if ($str[2]!=null && $str[2]!="")
          {

           $this->researchsattribute=Doctrine_Query::create()
		  ->from('observationdepth od')
		  ->where('od.id In ('.$str[2].')')
                  ->execute();
                  
          }


          if ($str[1]!=null && $str[1]!="")
          {
           $this->overlayImage=Doctrine_Query::create()
                  ->from('researchoverlay ro')
                   ->where('ro.id In ('.$str[1].')')
                   ->execute();
          }
      }




  }

  public function executeAutocomplete(sfWebRequest $request)
  {
    

    
          $researchs=Doctrine_Query::create()
                  ->select('s.id,s.Species')
                  ->from('Species s')
                  ->leftJoin('s.Observationdepthdetail odd')
                  ->orderby('s.species')
                  ->execute();
          $str="";
                   foreach($researchs as $r)
                   {
                    $str=$str.$r->getSpecies()."###";
                   }
                    echo $str;

		  return sfView::NONE;
  }


  public function executeExcelData(sfWebRequest $request)
  {
    $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {

          $str=$this->getRequestParameter('search');

          $this->researchs=Doctrine_Query::create()
                  ->from('observationdepthdetail odd')
		  ->leftJoin('odd.Observationdepth od')
		  ->leftJoin('od.Observationdepthattribute oda')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Entity rr')
                  ->leftJoin('odd.Species s')
                  ->leftJoin('s.Unit u')
                  ->leftJoin('s.Tags t')
                  ->leftJoin('od.Unit od_u')
                  ->leftJoin('oda.Attribute at')
                  ->leftJoin('oda.Unit oda_u')
                  ->where('odd.id In ('.$str.')')
                  ->orderby('o.Location,s.species')
                  ->execute();
      }


  }
  public function executeZipData(sfWebRequest $request)
  {



       $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {
          $temp_file = sys_get_temp_dir();
          mkdir($temp_file ."rbis" , 0777);
          $str = explode("OLY", $this->getRequestParameter('search'));
          if($str[0]!=null && $str[0]!="" && ($str[1]==null || $str[1]==""))
          {
              $this->redirect('searchresult/ExcelData?search='.$str[0]);
          }
          else
          {
                $archive_file_name= "rbis.zip";
                $zip = new ZipArchive();
                 if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
                    exit("cannot open <$archive_file_name>\n");
                  }
                
                if ($str[0]!=null && $str[0]!="")
                {
                
               $i = file_get_contents('http://'.$_SERVER['SERVER_ADDR'].'/searchresult/ExcelData?search='.$str[0]);
                $f = fopen($temp_file."rbis/".'data.xls','w+');
                fwrite($f,$i);
                fclose($f);
                $zip->addFile($temp_file."rbis/".'data.xls',"data.xls");
                
                }
                if ($str[1]!=null && $str[1]!="")
                {
                     $tmp=explode(",", $str[1]);
                     
                     foreach ($tmp as $t)
                     {

                        $iData = file_get_contents('http://'.$_SERVER['SERVER_ADDR'].'/searchresult/Overlay_Image_Download?overlayid='.$t);
                        $iData=explode("OLYOLY", $iData);
                        $i=$iData[2];
                        $f = fopen($temp_file.'rbis\\'.$iData[0].'.'.$iData[1],'w+');
                        fwrite($f,$i);
                        fclose($f);
                        $zip->addFile($temp_file.'rbis\\'.$iData[0].'.'.$iData[1],$iData[0].'.'.$iData[1]);
                        
                     }



                }
$zip->close();
if (is_dir($temp_file ."rbis")) {
    rmdir($temp_file ."rbis");

}
                  header("Content-type: application/zip");
                  header("Content-Disposition: attachment; filename=$archive_file_name");
                  header("Pragma: no-cache");
                  header("Expires: 0");
                  readfile("$archive_file_name");
                  exit;

          }
      }


return sfView::NONE;
  }
public function executeOverlay_Image(sfWebRequest $request)
  {
     $overlayid = 0;
      /**
       * IF PASSED PARAMETER IS NULL/0, THEN GET THE ID FROM SESSION
       */
      if($this->getRequestParameter('overlayid') != null && $this->getRequestParameter('overlayid') != '0')
      {
          $overlayid = $this->getRequestParameter('overlayid');
      $overlayList = Doctrine_Core::getTable('researchoverlay');
      $img = $overlayList->find($overlayid);
      //$entity = $entityList->find($this->getUser()->getAttribute('userid'));


      echo $img["overlayimage"];
      }


      return sfView::NONE;
  }
  public function executeOverlay_Image_Download(sfWebRequest $request)
  {
     $overlayid = 0;
      /**
       * IF PASSED PARAMETER IS NULL/0, THEN GET THE ID FROM SESSION
       */
      if($this->getRequestParameter('overlayid') != null && $this->getRequestParameter('overlayid') != '0')
      {
      $overlayid = $this->getRequestParameter('overlayid');
      $overlayList = Doctrine_Core::getTable('researchoverlay');
      $img = $overlayList->find($overlayid);
      //$entity = $entityList->find($this->getUser()->getAttribute('userid'));


      echo $img["overlaytitle"]."OLYOLY".$img["overlaytype"]."OLYOLY".$img["overlayimage"];
      }


      return sfView::NONE;
  }
 }